//cookie操作通用js
/**
 * 设置Cookie
 * @author  xiaogq
 * @param   key        cookie键
 * @param   value      cookie值
 * @param   time       cookie过期时间(s:表示秒、m:表示分钟、h:表示小时、d:表示天;例s20：表示20秒)
 */
function setCookie(key,value,time){
    var strsec = getsec(time);
    var exp = new Date();
    exp.setTime(exp.getTime() + strsec*1);
    document.cookie = key + "="+ escape (value) + ";expires=" + exp.toGMTString();
}

//获取cookie过期时间默认是一个小时(xiaogq)
function getsec(str){
    if(str != null && str != ""){
        var str1=str.substring(1,str.length)*1;
        var str2=str.substring(0,1);
        if (str2=="s"){
            return str1*1000;
        } else if (str2=="h"){
            return str1*60*60*1000;
        } else if (str2=="d"){
            return str1*24*60*60*1000;
        }
    }else{
        return 1*60*60*1000;
    }
}

/**
 * 根据key获取Cookie值
 * @author  xiaogq
 * @param   key        cookie键
 */
function getCookie(key){
    //获取所有的cookie键值
    var cookieKyeVals = document.cookie;
    if (cookieKyeVals != undefined && cookieKyeVals != null && cookieKyeVals.length > 0){
        var keyVals = cookieKyeVals.split(";");
        for(var i = 0 ; i < keyVals.length; i++){
            var vals = keyVals[i].split("=");
            if(trim(vals[0]) == key){
                return unescape(vals[1]);
            }
        }
        return "";
    }else{
        return "";
    }
}

/**
 * 根据可以删除cookies值
 * @author  xiaogq
 * @param   key        cookie键
 */
function delCookie(key){
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(key);
    if(cval != null){
        document.cookie = key + "="+cval+";expires=" + exp.toGMTString();
    }
}
